package com.javabasic.algorithm.leetcode;

/**
 * @Author xiongmin
 * @Description //TODO
 * @Date 2020/7/7 20:27
 * @Version 1.0
 **/
public class LinkedListCycle {

    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null || head.next.next == null) return false;
        ListNode signalNode = head.next, doubleNode = head.next.next;
        while (signalNode != null && doubleNode != null) {
            if (signalNode == doubleNode) {
                return true;
            }
            signalNode = signalNode.next;
            if (doubleNode.next != null) {
                doubleNode = doubleNode.next.next;
            } else {
                doubleNode = null;
            }
        }
        return false;
    }
}
